class Solution1 {
    public boolean[][] fg;
    public int m;
    public int n;
    public void solve(char[][] board) {
        m=board.length;
        n=board[0].length;
        fg=new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if((i==0 || j==0 || i==m-1 || j==n-1) && board[i][j]=='O'){
                    fg[i][j]=true;
                    dfs(board,i,j);
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(board[i][j]=='O' && !fg[i][j]){
                    board[i][j]='X';
                }
            }
        }
    }

    private void dfs(char[][] board, int a, int b) {
        int[] dx={1,0,-1,0};
        int[] dy={0,1,0,-1};
        for (int i = 0; i < 4; i++) {
            int x=dx[i]+a;
            int y=dy[i]+b;

            if(x>=0 && x<m && y>=0 && y<n && board[x][y]=='O'){
                fg[x][y]=true;
                dfs(board,x,y);
            }
        }
    }
}